package com.weilan.blog.admin.controller;


import com.weilan.blog.admin.service.BlogSubjectService;
import com.weilan.blog.admin.vo.BlogSubjectVO;
import com.weilan.blog.base.exception.ThrowableUtils;
import com.weilan.blog.base.validator.group.Delete;
import com.weilan.blog.base.validator.group.GetList;
import com.weilan.blog.base.validator.group.Insert;
import com.weilan.blog.base.validator.group.Update;
import com.weilan.blog.common.annotion.AvoidRepeatableCommit.AvoidRepeatableCommit;
import com.weilan.blog.common.global.SysConf;
import com.weilan.blog.utils.ResultUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @description:
 * @author: 孙 峰 a
 * @time: 2020/9/27 23:54
 */
@Slf4j
@RestController

@RequestMapping("/blogSubject")
public class BlogSubjectController {
    @Autowired
    private BlogSubjectService subjectService;

   @PostMapping("/getList")
    public String getList(@Validated({GetList.class}) @RequestBody BlogSubjectVO subjectVO, BindingResult result) {

        ThrowableUtils.checkParamArgument(result);
        return ResultUtil.result(SysConf.SUCCESS, subjectService.getPageList(subjectVO));
    }

    @AvoidRepeatableCommit
   @PostMapping("/add")
    public String add(@Validated({Insert.class}) @RequestBody BlogSubjectVO subjectVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return subjectService.addSubject(subjectVO);
    }


    @PostMapping("/edit")
    public String edit(@Validated({Update.class}) @RequestBody BlogSubjectVO subjectVO, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return subjectService.editSubject(subjectVO);
    }

    @PostMapping("/deleteBatch")
    public String delete(@Validated({Delete.class}) @RequestBody List<BlogSubjectVO> subjectVOList, BindingResult result) {

        // 参数校验
        ThrowableUtils.checkParamArgument(result);
        return subjectService.deleteBatchSubject(subjectVOList);
    }
}
